import type { Ref } from 'vue';
import type { GetClinicScheduleRespVO } from '@/api/clinic-schedule';
import { computed, watch } from 'vue';
import { RegistrationProjectFeeApi } from '@/api/registration-project-fee';
import { useRequest } from '@/hooks/useRequest';

export function useRegistrationProjectFee(doctor: Ref<GetClinicScheduleRespVO>) {
  // 获取挂号项目费用列表
  const {
    data: feeList,
    runAsync: getRegistrationProjectFeeList
  }
= useRequest(() => RegistrationProjectFeeApi.getRegistrationProjectFeeList({
  professionalTitleId: doctor.value?.professionalTitle as unknown as string,
  sectionId: doctor.value?.sectionId as unknown as number
}), {
  manual: true
});

  // 费用项目列表
  const feeItems = computed(() => [
    {
      title: '挂号费',
      price: Number(feeList.value?.registrationFee || 0).toFixed(2)
    },
    {
      title: '治疗费',
      price: Number(feeList.value?.treatmentFee || 0 || 0).toFixed(2)
    },
    {
      title: '其他费用',
      price: Number(feeList.value?.otherFee || 0).toFixed(2)
    }
  ]);

  // 总金额
  const totalAmount = computed(() => {
    return feeItems.value.reduce((prev, cur) => {
      return prev + Number(cur.price || 0);
    }, 0).toFixed(2);
  });

  watch(doctor, () => {
    if (doctor.value) {
      getRegistrationProjectFeeList();
    }
  }, {
    immediate: true
  });

  return {
    feeList,

    feeItems,

    totalAmount
  };
}
